﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Data.SQLite;
using PriErp.Data;
using PriErp.Tools.Extensions;

namespace PriErp.Dal
{
    /// <summary>
    /// 历史记录
    /// </summary>
    [Serializable]
    public class History
    {
        #region Model
        /// <summary>
        /// 交易流水号
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 交易人ID
        /// </summary>
        public int UserID { get; set; }
        /// <summary>
        /// 分类ID
        /// </summary>
        public int CategoryID { get; set; }
        /// <summary>
        /// 交易金额
        /// </summary>
        public double Money { get; set; }
        /// <summary>
        /// 余额
        /// </summary>
        public double Total { get; set; }
        /// <summary>
        /// 交易类型(-1:支出,1:收入)
        /// </summary>
        public int TransType { get; set; }
        /// <summary>
        /// 交易时间
        /// </summary>
        public DateTime TransTime { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        #endregion Model
    }

    /// <summary>
    /// History操作类
    /// </summary>
    public static class HistoryDAL
    {
        #region  BasicMethod
        /// <summary>
        /// 添加纪录
        /// </summary>
        /// <param name="model">记录</param>
        /// <returns>是否成功添加</returns>
        public static bool Add(History model)
        {
            string strSql = @"insert into History
                              (UserID,CategoryID,Money,TransType,TransTime,Remark)
                            values (@UserID,@CategoryID,@Money,@TransType,@TransTime,@Remark)";
            //(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = { new SQLiteParameter("@UserID",model.UserID),
                                             new SQLiteParameter("@CategoryID", model.CategoryID),
                                             new SQLiteParameter("@Money", model.Money),
                                             new SQLiteParameter("@TransType", model.TransType),
                                             new SQLiteParameter("@TransTime", model.TransTime),
                                             DbHelperSQLite.MakeParams("@Remark", DbType.String,200,model.Remark),};
            return DbHelperSQLite.ExecuteSql(strSql, parameters) > 0;
        }

        /// <summary>
        /// 更新选中记录
        /// </summary>
        /// <param name="model">选中记录</param>
        /// <returns>是否更新成功</returns>
        public static bool Update(History model)
        {
            string strSql = @"update History set UserID=@UserID,CategoryID=@CategoryID,
                              Money=@Money,TransType=@TransType,TransTime=@TransTime,
                              Remark=@Remark where ID=@ID";
            SQLiteParameter[] parameters = { new SQLiteParameter("@UserID", model.UserID),
                                             new SQLiteParameter("@CategoryID", model.CategoryID),
                                             new SQLiteParameter("@Money", model.Money),
                                             new SQLiteParameter("@TransType", model.TransType),
                                             new SQLiteParameter("@TransTime", model.TransTime),
                                             new SQLiteParameter("@Remark", model.Remark),
                                             new SQLiteParameter("@ID", model.ID)};
            return DbHelperSQLite.ExecuteSql(strSql, parameters) > 0;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="Ids">需要删除的记录ID</param>
        /// <returns>是否删除成功</returns>
        public static bool Delete(List<int> Ids)
        {
            string strSql = string.Format("delete from History  where ID in({0})", Ids.ToUninString(','));
            return DbHelperSQLite.ExecuteSql(strSql) > 0;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="strWhere">过滤参数</param>
        /// <returns>返回DataTable数据集合</returns>
        public static DataTable GetList(string strWhere)
        {
            //TODO:根据需求显示余额的话需要从试图缓存中取
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UserID,CategoryID,Money,TransType,TransTime,Remark,CreateTime ");
            strSql.Append(" FROM History ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="strWhere">过滤参数</param>
        /// <returns>返回记录总数</returns>
        public static int GetRecordCount(string strWhere)
        {
            //TODO:根据需求显示余额的话需要从试图缓存中取
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM History ");
            if (strWhere.Trim() != "")
                strSql.Append(" where " + strWhere);
            return DbHelperSQLite.GetSingle(strSql.ToString()).ToStringEmpty().ToInt32();
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="strWhere">过滤参数</param>
        /// <param name="orderby">排序参数</param>
        /// <param name="startIndex">起始页</param>
        /// <param name="endIndex">终止页</param>
        /// <returns>返回DataTable数据集合</returns>
        public static DataTable GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            //TODO:根据需求显示余额的话需要从试图缓存中取
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from History T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQLite.Query(strSql.ToString());
        }
        #endregion  BasicMethod
    }
}
